//
//  GHForTestVC.swift
//  GHSwiftProject
//
//  Created by Guanghui Liao on 6/11/18.
//  Copyright © 2018 liaoworking. All rights reserved.
//

import UIKit
import DZNEmptyDataSet
class GHForTestVC: UIViewController {

    @IBOutlet weak var BGTableView: UITableView!
    
    let viewModel = GHForTestViewModel()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationItem.title = "首页"
        configTableView()
        loadData()
    }
    
    /// 配置tableView
    func configTableView() {
        BGTableView.rowHeight = UITableViewAutomaticDimension
        BGTableView.register(UINib(nibName: "GHItemCell", bundle: nil), forCellReuseIdentifier: "GHItemCell")
        BGTableView.delegate = self
        BGTableView.dataSource = self
        BGTableView.separatorStyle = .none
        BGTableView.emptyDataSetSource = self
    }
    
    /// 请求数据
    func loadData() {
        viewModel.loadData {
            self.BGTableView.reloadData()
        }
    }
    
    
    /// 按钮点击事件
    ///
    /// - Parameter position: 按钮的位置
    func didClickBtn(_ position:btnClickPosition) {
        switch position {
        case .left:
            navigationController?.pushViewController(GHAViewController())
        case .right:
            navigationController?.pushViewController(GHBViewController())
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
}
//MARK: - UITableViewDelegate & dataSource
extension GHForTestVC:UITableViewDelegate,UITableViewDataSource{
   
    //MARK: - tableViewDelegate and dataSource
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return viewModel.modelList.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "GHItemCell", for: indexPath) as! GHItemCell
        cell.model = viewModel.modelList[indexPath.row]
        cell.selectionStyle = .none
        //按钮的点击事件闭包
        cell.btnClickClosure = { position in
            self.didClickBtn(position)
        }
        return cell
    }
}

//空白占位图代理
extension GHForTestVC: DZNEmptyDataSetSource{
    public func title(forEmptyDataSet scrollView: UIScrollView!) -> NSAttributedString! {
        
        let MAString = NSAttributedString(string: "暂无数据", attributes: [NSAttributedStringKey.font:UIFont.systemFont(ofSize: 17),NSAttributedStringKey.foregroundColor:UIColor.init(red: 104, green: 164, blue: 237) ?? UIColor.black])
        return MAString
    }
    
    
    public func image(forEmptyDataSet scrollView: UIScrollView!) -> UIImage! {
        return  #imageLiteral(resourceName: "不限次数")
    }
}
